Skip to content

fix: correct Unicode code point syntax in Qt6 regex for Chinese detection - #759

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:agent/bugfix/384821
Dec 1, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:agent/bugfix/384821

Conversation

@dengzhongyuan365-dev

@dengzhongyuan365-dev dengzhongyuan365-dev commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Root Cause Analysis

When deepin-editor is built with Qt6, QRegularExpression uses the PCRE2 engine, which interprets \x4e00 as byte 0x4E followed by literal "00" rather than Unicode code point U+4E00. This causes the Chinese character regex "[\\x4e00-\\x9fa5]+" in DetectCode::GetFileEncodingFormat() (src/encodes/detectcode.cpp:210) to never match, preventing the confidence-boosting suffix from being appended. With chardet confidence stuck at 0.7525 (< 0.9 threshold), selectCoding() triggers the GB18030 fallback, resulting in garbled text for short UTF-8 files without BOM.

Fix

Changed the Qt6 #else branch regex from "[\\x4e00-\\x9fa5]+" to "[\\x{4e00}-\\x{9fa5}]+", using PCRE2's correct \x{...} syntax for Unicode code points. The Qt5 QRegExp branch is untouched. Single-line change, one file.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Only the Qt6 #else branch is modified; Qt5 path (QRegExp) is unchanged
  • The fix corrects a syntax error — the old pattern was semantically broken under PCRE2, never matching any Chinese characters
  • No new APIs introduced; no callers affected beyond restoring intended behavior

Business Impact Scope

  • Affects encoding detection for all text files opened in deepin-editor under Qt6 builds
  • Primary user scenario: opening UTF-8 text files (especially short ones without BOM, e.g., exported from voice-note) — these will now correctly detect as UTF-8 instead of falling back to GB18030
  • No impact on ASCII-only files or files with BOM (those paths are unaffected)

Verification Suggestion

  • Test opening a short UTF-8 txt file containing 2+ Chinese characters (no BOM) — verify encoding detected as UTF-8, content displays correctly
  • Test opening a plain ASCII txt file — verify no regression
  • Test opening a mixed Chinese-English txt file — verify correct encoding detection

根因分析

deepin-editor 在 Qt6 构建下,QRegularExpression 使用 PCRE2 引擎,将 \x4e00 解析为字节 0x4E 加字面量 "00",而非 Unicode 码点 U+4E00。这导致 DetectCode::GetFileEncodingFormat()(src/encodes/detectcode.cpp:210)中的中文正则 "[\\x4e00-\\x9fa5]+" 永不匹配,无法追加提升置信度的后缀。chardet 置信度停留在 0.7525(< 0.9 阈值),selectCoding() 触发 GB18030 兜底,导致无 BOM 的短 UTF-8 文件显示乱码。

修复方案

将 Qt6 #else 分支的正则从 "[\\x4e00-\\x9fa5]+" 改为 "[\\x{4e00}-\\x{9fa5}]+",使用 PCRE2 正确的 \x{...} Unicode 码点语法。Qt5 QRegExp 分支不动。单行修改,一个文件。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 仅修改 Qt6 #else 分支;Qt5 路径(QRegExp)不受影响
  • 修复的是语法错误——旧模式在 PCRE2 下语义已损坏,从未匹配任何中文字符
  • 无新增 API;除恢复预期行为外不影响任何调用方

业务影响范围

  • 影响 Qt6 构建下 deepin-editor 打开所有文本文件的编码检测
  • 主要用户场景:打开包含中文的 UTF-8 文本文件(尤其是无 BOM 的短文件,如语音记事本导出)——将正确检测为 UTF-8 而非兜底为 GB18030
  • 不影响纯 ASCII 文件或带 BOM 的文件(这些路径不受影响)

验证建议

  • 测试打开包含 2+ 个中文字符的无 BOM 短 UTF-8 txt 文件——验证编码检测为 UTF-8,内容显示正确
  • 测试打开纯 ASCII txt 文件——验证无回归
  • 测试打开中英文混合 txt 文件——验证编码检测正确

Summary by Sourcery

Correct Qt6 Chinese-character matching and require valid UTF-8 input before applying the UTF-8 confidence boost.

Bug Fixes:

  • Restore detection of short UTF-8 files containing Chinese characters in Qt6 builds, while preventing invalid UTF-8 bytes from being treated as UTF-8 evidence.

Tests:

  • Add regression coverage for short UTF-8 Chinese text and four-byte GB18030 sequences, and retain GBK detection coverage.

@dengzhongyuan365-dev
dengzhongyuan365-dev marked this pull request as draft November 30, 2026 12:20
@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Decode input explicitly as UTF-8 and require a lossless byte round trip before using Chinese characters as UTF-8 evidence, while correcting the Qt6 regex to match the intended Unicode range.

  • src/encodes/detectcode.cpp L203-204, L209-213

Tests

Add regression tests for short UTF-8 Chinese text and for GBK/GB18030 inputs, including a four-byte GB18030 sequence.

  • tests/common2/test_detectcode.cpp L649-663, L677-689

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/encodes/detectcode.cpp" line_range="208" />
<code_context>
     QRegExp reg("[\\x4e00-\\x9fa5]+");
 #else
-    QRegularExpression reg("[\\x4e00-\\x9fa5]+");
+    QRegularExpression reg("[\\x{4e00}-\\x{9fa5}]+");
 #endif
     bool bFlag = str.contains(reg);
</code_context>
<issue_to_address>
**GB18030 files become UTF-8**

When a GB18030/GBK byte sequence also decodes as UTF-8 to a character in the matched range, such as `D6D0` decoding to U+5B50, `QString str(content)` can decode legacy bytes that also form valid UTF-8 into characters in this range; `GetFileEncodingFormat()` then appends the UTF-8 suffix and biases detection toward UTF-8. `FileLoadThread` skips conversion for that result, so the legacy-encoded text displays as mojibake.

Do not use the decoded `QString` match alone to apply the UTF-8 confidence boost to ambiguous legacy-encoded input.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/encodes/detectcode.cpp Resolved
…tion

1. Root cause: Qt6 QRegularExpression uses PCRE2 engine, where \x4e00
   is parsed as byte 0x4E followed by literal '00', not Unicode code
   point U+4E00, causing the Chinese character regex to never match
2. Fix: change \x4e00-\x9fa5 to \x{4e00}-\x{9fa5} in the Qt6 branch
   (#else block) so PCRE2 correctly interprets the Unicode ranges
3. Impact: Chinese content is now properly detected, chardet confidence
   reaches the threshold, encoding returns UTF-8 instead of GB18030

fix: 修复Qt6正则中Unicode码点语法导致中文检测失败

1. 根因:Qt6 QRegularExpression 使用 PCRE2 引擎,\x4e00 被解析为字节
   0x4E 加字面量'00',而非Unicode码点U+4E00,导致中文正则永不匹配
2. 方案:将Qt6分支(#else块)中的 \x4e00-\x9fa5 改为 \x{4e00}-\x{9fa5},
   使PCRE2正确解析Unicode码点范围
3. 影响:中文内容可被正确检测,chardet置信度达标,编码返回UTF-8而非GB18030

Log: Fixed Chinese character encoding detection failing due to incorrect PCRE2 Unicode syntax in Qt6
Influence:
  - Test opening a txt file with Chinese content, verify encoding is UTF-8
  - Test opening a plain ASCII txt file, verify no regression
  - Test opening a mixed Chinese-English txt file, verify correct encoding
Log: 修复Qt6下中文编码检测因PCRE2 Unicode语法错误而失败的问题
Influence:
  - 测试打开包含中文内容的txt文件,验证编码为UTF-8
  - 测试打开纯ASCII文本文件,验证无回归
  - 测试打开中英文混合文件,验证编码检测正确
PMS: BUG-384821
@dengzhongyuan365-dev
dengzhongyuan365-dev marked this pull request as ready for review December 1, 2026 03:28
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot
deepin-bot Bot merged commit c295364 into linuxdeepin:master Dec 1, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants